package com.lagou.edu.service.impl;

import com.lagou.edu.dao.CodeDao;
import com.lagou.edu.pojo.Code;
import com.lagou.edu.service.CodeService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;

import java.util.Date;
import java.util.Optional;

@Service
public class CodeServiceImpl implements CodeService {

    @Autowired
    private CodeDao codeDao;

    @Override
    public int validateCode(String email, String codeStr) {

        Code code = new Code();
        code.setEmail(email);
        code.setCode(codeStr);
        Example<Code> example = Example.of(code);
        Optional<Code> optional = codeDao.findOne(example);
        if (optional.isPresent()){
            Date expiretime = optional.get().getExpiretime();
            if (expiretime.before(new Date())){
                return 2;
            } else {
                return 0;
            }
        }

        return 1;
    }
}
